app.controller("z-closed-c", [ '$scope','$rootScope','$z-http','$sce','z-closed-s','z-attendance-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.editModel = {};
		$scope.editModel.title="";
		$scope.editModel.closed={};
		$scope.viewModel.yearData = [];
		var newYear = new Date().getFullYear();
		for(var year = newYear-10;year<=newYear+10;year++ ){
			$scope.viewModel.yearData.push(year);
		}
		//查询页面名称
		MenuService.getMenuByUrl({url:'closed'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		//加载新增页月份数据
		$scope.editModel.bindSelectData = function(){
				$scope.editModel.closeMonthData = $scope.editModel.canUseMonth;
		};
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getClosedPageList({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage
			},
			function(data){
				$scope.viewModel.dataList = data.closedBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//修正
		$scope.viewModel.add=function(){
			$scope.editModel.title="设置封帐";
			$scope.editModel.closed={};
			$scope.editModel.closed.closedYear = newYear+"";
			//$scope.editModel.closed.closedYear= $filter("date")( new Date(), "yyyy");
			$scope.viewModel.load();
			$scope.editForm.closeMonth.$dirty=false;
		};
		
		//验证
		$scope.viewModel.validForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.closeMonth.$dirty){
            		$scope.editForm.closeMonth.$dirty=true;
            	}
                return true;
            }else{
            	return false;
            }
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.validForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
				Service.saveClosed($scope.editModel.closed,
						function(data){
							$scope.viewModel.unLoad();
							$rootScope.alertInfo("设置成功。");
							$scope.viewModel.bindData();
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
		};
		
		//删除
		$scope.viewModel.delele=function(closed){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteClosed({
					closedId:closed.closedId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		$scope.viewModel.checkGlobal=function(){
			return false;
		};
} ]);